/*
 * @Author: GY19531 418941369@qq.com
 * @Date: 2022-09-01 18:10:26
 * @LastEditors: GY19531 418941369@qq.com
 * @LastEditTime: 2022-09-02 17:07:41
 * @FilePath: \one-web\src\pages\RemindManage\mview\stopManageView.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import React, { Ref, useEffect, useMemo, useRef, useState } from 'react';
import { OneModal } from '@/components/one-ui';
import { Modal, Input, Form, Spin, message } from 'antd';
import { RemindManageAPI } from '../common/type';
import { Select } from 'antd';
import { connect, useRequest } from 'umi';
import { RemindManageService, stopNotifyService } from '../services';
import { loadMdicMap } from '@/services/BaseLayout';
import pageStyles from '@/common/pageLayout.less';
import { IOneModalRef } from '@/pages/type';
import {
  getCompanyID,
  getPosIDs,
  getTenantID,
  getUserDepID,
  getUserID,
  requestFunComponFiex,
} from '@/utils';
import moment from 'moment';
const { Option } = Select;
const { TextArea } = Input;
const StopManageView: React.FC<RemindManageAPI.IStopRemindModal> = ({
  onClose,
  checkRowObj,
  reloadManage,
}) => {
  /**
   * 表单 form
   */
  const [stopForm] = Form.useForm();
  const [form, setform]: any = useState({
    cancelReason: `${getTenantID}1403`,
    cancelType: '',
  });
  const stopCallback = (res: any) => {
    if (res && res?.msgFlag) {
      message.success(res.msg);
      reloadManage();
      onCloseModal();
    } else {
      message.error(res?.msg);
    }
  };
  const { stopNotifyLoading, stopNotifyRun } = stopNotifyService(stopCallback);
  /**
   *  弹框的ref
   */
  const stopRemindModalRef: Ref<IOneModalRef> = useRef(null);
  /**
   * 关闭弹窗
   */
  const onCloseModal = () => {
    if (stopRemindModalRef) {
      if (stopRemindModalRef.current) {
        if (stopRemindModalRef.current.onModalCancel) {
          stopRemindModalRef.current.onModalCancel();
        }
      }
    }
  };
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap([`${getTenantID()}1403`]);
  });

  const loadMapList = useMemo(() => {
    return loadMdicMapList?.data?.obj;
  }, [loadMdicMapList?.data]);
  return (
    <OneModal
      title={'停止提醒'}
      ref={stopRemindModalRef}
      width={document?.body?.clientWidth * 0.25}
      open={true}
      centered={true}
      transitionName=""
      bodyStyle={{
        padding: '10px 0px',
      }}
      onCancel={onClose}
      onOk={(e) => {
        stopForm?.validateFields()?.then((values: any) => {
          let obj = {
            ...values,
            id: checkRowObj?.id,
            cancelerID: getUserID(),
            cancelPositionID: getPosIDs(),
            cancelDepartmentID: getUserDepID(),
            cancelCompanyID: getCompanyID(),
            paramObject: {
              executeStyle: 1,
              cronExpression: null,
              quartzID:
                moment(new Date()).format('YYYYMMDDHHmmss') +
                Math.floor(Math.random() * 900 + 100) +
                '',
              quartzType: checkRowObj?.notifyType,
              jobClassName: 'cn.onefox.job.core.remind.RemindJob',
            },
            versionNum: checkRowObj?.versionNum,
          };

          stopNotifyRun(obj);
        });
      }}
      confirmLoading={loadMdicMapList?.loading || stopNotifyLoading}
    >
      <Spin spinning={loadMdicMapList?.loading || stopNotifyLoading}>
        <div className={pageStyles['fromView-styles']}>
          <Form form={stopForm}>
            <Form.Item
              name="cancelType"
              label="停止类型"
              rules={[
                {
                  required: true,
                  message: '请选择取消类型',
                },
              ]}
            >
              <Select
                placeholder="请选择取消类型"
                value={form.cancelReason}
                onChange={(e) => {}}
              >
                {loadMapList?.[`${getTenantID()}1403`]?.map(
                  (item: Record<string, any>) => {
                    return (
                      <Option key={item?.id} value={item?.id}>
                        {item?.name}
                      </Option>
                    );
                  },
                )}
              </Select>
            </Form.Item>
            <Form.Item
              name="cancelReason"
              label="停止原因"
              rules={[
                {
                  required: true,
                  message: '请输入取消原因',
                },
              ]}
            >
              <TextArea
                onChange={(e) => {
                  setform({ ...form, cancelType: e.target.value });
                }}
                rows={4}
              />
            </Form.Item>
          </Form>
        </div>
      </Spin>
    </OneModal>
  );
};

export default StopManageView;
